package com.bible.fire.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import android.text.format.DateFormat;
import android.util.Log;

public class TimeUtil {
	
	private static final String TAG = TimeUtil.class.getSimpleName();
    
	/**
	 * 获取两个时间之间的间隔
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static Map<String,Long> getDuration(Date startTime, Date endTime){		
		Map<String, Long> durationMap = new HashMap<String, Long>();
		try {
			long diff = endTime.getTime() - startTime.getTime();//这样得到的差值是微秒级别
			long days = diff / (1000 * 60 * 60 * 24);
			long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
            long seconds = ( diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60) - minutes * (1000 * 60) )/1000;
            durationMap.put( "d", days );
            durationMap.put( "h", hours );
            durationMap.put( "m", minutes );
            durationMap.put( "s", seconds );
		} catch (Exception e) {
			Log.e(TAG, "", e);
		}
		return durationMap;
	}
	
	public static String getTimeStr(Date date, String format){
		return DateFormat.format( format, date).toString();
	}
	
	public static String getTimeStr(Calendar calendar, String format){
		return DateFormat.format( format, calendar).toString();
	}
	
	public static String getSerialNoFromTime(){
		return DateFormat.format("yyyyMMddhhmmssSSS",Calendar.getInstance(Locale.CHINA)).toString();
	}
	
}
